package com.sf.rbac.web.controller;


import com.github.pagehelper.PageInfo;
import com.sf.rbac.domain.Role;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IRoleService;
import com.sf.rbac.util.JsonResult;
import com.sf.rbac.util.PageResult;
import com.sf.rbac.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    public JsonResult list(QueryObject queryObject) {
        //PageInfo 帮我们封装分页数据到pageInfo中  pageInfo是pageHelper的对象
        PageInfo<Role> pageInfo = roleService.query(queryObject);
        // PageResult 中有想要返回的数据  创建一个PageResult对象
        PageResult result = new PageResult();
        // 将pageInfo中封装的数据传给result中 返回给前端
        BeanUtils.copyProperties(pageInfo, result);
        return JsonResult.success(result);
    }

    @DeleteMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        roleService.delete(id);
        return JsonResult.success();
    }

    @GetMapping("/info/{id}")
    public JsonResult info(@PathVariable Long id) {
        Role role = roleService.get(id);
        return JsonResult.success(role);
    }

    @GetMapping("/listAll")
    public JsonResult listAll() {
        List<Role> list = roleService.listAll();
        return JsonResult.success(list);
    }

    @GetMapping("/query/{id}")
    public JsonResult query(@PathVariable Long id) {
        List<Role> list = roleService.queryRolesByEmpId(id);
        return JsonResult.success(list);
    }

    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody RoleVo roleVo){
        roleService.saveOrUpdate(roleVo);
        return JsonResult.success();
    }
}




